package dbTools.coeModel.modelElement.demos.demo1;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import projectsupporter.common.logic.GlobalInfo;
import dbTools.DBToolEngine;
import dbTools.coeModel.annotation.FillItem;
import dbTools.coeModel.modelElement.AbstractReplaceElement;
import dbTools.tableInfo.field.Field;
import dbTools.tableInfo.field.fieldInfo.type.types.TypeReference;
import dbTools.tableInfo.info.TableInfo;

public class DemoView1 extends AbstractReplaceElement {
	
	@FillItem(itemName = "<##MAST_NAME##>")
	public String fillItemTest(TableInfo tableInfo)
	{
		return tableInfo.getTableName();
	}
	
	@FillItem(itemName = "<##MAST_ID##>")
	public String fillItemTest2(TableInfo tableInfo)
	{
		return tableInfo.getKey();
	}
	
	@FillItem(itemName = "<##MAST_FIELD##>")
	public String fillItemTest3(TableInfo tableInfo)
	{
		return tableInfo.getTableId().toLowerCase();
	}
	
	@FillItem(itemName = "<##MAINCODE##>")
	public String fillItemTest4(TableInfo tableInfo)
	{
		List<String> ignoreList = new ArrayList<String>();
		ignoreList.add("insTime");
		ignoreList.add("insPcNm");
		ignoreList.add("insTanNm");
		ignoreList.add("updTime");
		ignoreList.add("updPcNm");
		ignoreList.add("updTanNm");
		
		StringBuffer code= new StringBuffer();
		
		code.append("	<table id=\"fieldTable\">\r\n");
		
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		
		String objId=tableInfo.getTableId().toLowerCase();
		
		int count=0;
		for(Map.Entry<String, Field> entry: tableInfo.getAllFields().entrySet())
		{
			Field field=entry.getValue();
			String idStirng1=(field.isKey()&&isMkey)?"id_":"";
			String idStirng2=(field.isKey()&&isMkey)?"id.":"";
			
			if(ignoreList.contains(field.getFieldName()))
			{
				continue;
			}

			if(count%2==0)
			{
				code.append("		<tr>\r\n");
			}
			
			if(field.getDataTypeString().equals(Field.TYPE_DATE)
				||field.getImeType().toString().contains("date"))
			{
				code.append("			<td class=\"fieldItem\">"+field.getItemName()+":</td><td class=\"fieldValue\">"+
						"<sj:datepicker id='"+objId+"_"+idStirng1+field.getFieldName()+"' changeMonth='true' changeYear='true' size='8' " +
						"cssClass='datepicker' showOn='focus' cssStyle='ime-mode: inactive' " +
						"displayFormat='yymmdd'  " +
						"name='"+objId+"."+idStirng2+field.getFieldName()+"' /> </td>\r\n");
			}
			else
			{
				 TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);
				
				
				 if(ref==null)
				 {
					 code.append("			<td class=\"fieldItem\">"+field.getItemName()+":</td><td class=\"fieldValue\">"+
							 "<s:textfield id='"+objId+"_"+idStirng1+field.getFieldName()+"'" +
							 		"name='"+objId+"."+idStirng2+field.getFieldName()+"' /> </td>\r\n");
				 }
				 else
				 {
					 TableInfo refTable=DBToolEngine.findTable(ref.getReferenceId());
					 String fileName = refTable.getTableId().toLowerCase();
					 Field refField=refTable.findField(ref.getReferenceField());
					 
					 boolean isrefMkey=refTable.getKeyFields().size()>1;
					 
						String refidStirng1=(refField.isKey()&&isrefMkey)?"id_":"";
						String refidStirng2=(refField.isKey()&&isrefMkey)?"id.":"";
					 
					 String listValue=refidStirng2+refTable.findSimpleRefDisplayName();
					 listValue=listValue==""?refidStirng2+refField.getFieldName():listValue;
						
					 code.append("			<td class=\"fieldItem\">"+field.getItemName()+":</td><td class=\"fieldValue\">");
					 code.append("			<s:url id='reference_"+fileName+"' action='reference.do?refrenceId="+refTable.getKey()+"' namespace='/base'> \r\n");
					 code.append(" 			</s:url>\r\n");
					 code.append(" 				<sj:select\r\n");
					 code.append(" 				id='"+objId+"_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"'\r\n");
					 code.append(" 				name='"+objId+"."+idStirng2+field.getFieldName()+"."+refidStirng2+refField.getFieldName()+"'\r\n");
					 code.append(" 				href='%{reference_"+fileName+"}' \r\n");
					 code.append(" 				list='refrenceList'\r\n");
					 code.append(" 				listValue='"+listValue+"' \r\n");
					 code.append(" 				listKey='"+refidStirng2+refField.getFieldName()+"' \r\n");
					 //code.append(" 	autocomplete='true'\r\n");
					 code.append(" 				loadMinimumCount='2'\r\n /> </td>");
				 }
			}
			if(count%2==1)
			{
				code.append("		</tr>\r\n");
			}
			count++;
		}
		
		code.append("	</table>\r\n");
		
		return code.toString();
	}
	
	@FillItem(itemName = "<##GRID_DATA##>")
	public String fillItemTest5(TableInfo tableInfo)
	{
		StringBuffer code= new StringBuffer();
		
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		
		String objId=tableInfo.getTableId().toLowerCase();
		
		code.append("		<s:form id=\"ListForm\" action=\""+tableInfo.getTableId().toLowerCase()+"mast-getObj\" theme=\"simple\">\r\n");
		code.append("		<tangs:grid id=\"table\" width=\""+200*tableInfo.getAllFields().size()+"px\" cssClass=\"tablesorter\"  page=\"compageData\">\r\n");
		code.append("			<tangs:column width=\"20px\" type=\"radio\" title=\"&nbsp;\" name=\"rad\" cssClass=\"empty2\"/>\r\n");
		code.append("			<tangs:column width=\"40px\" type=\"serial\" title=\"番号\"/>\r\n");
		
		for(Map.Entry<String, Field> entry: tableInfo.getAllFields().entrySet())
		{
			Field field=entry.getValue();
			String idStirng1=(field.isKey()&&isMkey)?"id_":"";
			String idStirng2=(field.isKey()&&isMkey)?"id.":"";

			TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);
			
			//int width=20*field.getItemName().length();
			int width=200;
			
			if (ref == null) {
				code.append("			<tangs:column width=\""+width+"px\" property=\""+idStirng2+field.getFieldName()+"\" title=\""+field.getItemName()+"\"/>\r\n");
			} else {
				TableInfo refTable = DBToolEngine.findTable(ref
						.getReferenceId());
				Field refField = refTable.findField(ref.getReferenceField());

				boolean isrefMkey = refTable.getKeyFields().size() > 1;
				String refidStirng2 = (refField.isKey() && isrefMkey) ? "id." : "";

				String listValue = refidStirng2
						+ refField.getFieldName();

				code.append("			<tangs:column width=\""+width+"px\" property=\""+idStirng2+field.getFieldName()+"."+listValue+"\" title=\""+field.getItemName()+"\"/>\r\n");
						
			}

		}
		code.append("		</tangs:grid >\r\n");
		
		code.append(getPrpCode(tableInfo));
		
		code.append("		</s:form>\r\n");
		
		return code.toString();
	}

	private String getPrpCode(TableInfo tableInfo) {
		StringBuffer code= new StringBuffer();
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		
		
		for(Map.Entry<String, Field> entry: tableInfo.getKeyFields().entrySet())
		{
			Field field=entry.getValue();
			
			String idStirng1=(field.isKey()&&isMkey)?"id_":"";
			String idStr=isMkey?"."+field.getFieldName():"";
			
			TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);

			if (ref == null) {
				code.append("<input type=\"hidden\" name=\"objId"+idStr+"\" id=\"grid_"+idStirng1+field.getFieldName()+"\" style=\"display:none\"></input>\r\n");
			} else {
				TableInfo refTable = DBToolEngine.findTable(ref
						.getReferenceId());
				Field refField = refTable.findField(ref.getReferenceField());

				boolean isrefMkey = refTable.getKeyFields().size() > 1;
				String refidStirng2 = (refField.isKey() && isrefMkey) ? "id." : "";
				String refidStirng1 = (refField.isKey() && isrefMkey) ? "id_" : "";

				code.append("<input type=\"hidden\" name=\"objId"+idStr+"."+refidStirng2+refField.getFieldName()+"\" " +
						"id=\"grid_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"\" style=\"display:none\"></input>\r\n");		
			}

		}
		
		
		return code.toString();
	}

	@FillItem(itemName = "<##FILL_ID##>")
	public String fillItemTest6(TableInfo tableInfo)
	{
		StringBuffer code= new StringBuffer();
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		
		String idStr=isMkey?"objId.":"";
		for(Map.Entry<String, Field> entry: tableInfo.getKeyFields().entrySet())
		{
			Field field=entry.getValue();
			
			String idStirng1=(field.isKey()&&isMkey)?"id_":"";
			String idStirng2=(field.isKey()&&isMkey)?"id.":"";
			
			TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);

			if (ref == null) {
				code.append("$('#grid_"+idStirng1+field.getFieldName()+"').val($(this).parent().parent().children(\"td\").eq($('#grid_"+idStirng1+field.getFieldName()+"_index').val()).text());\r\n");
				
			} else {
				TableInfo refTable = DBToolEngine.findTable(ref
						.getReferenceId());
				Field refField = refTable.findField(ref.getReferenceField());

				boolean isrefMkey = refTable.getKeyFields().size() > 1;
				String refidStirng2 = (refField.isKey() && isrefMkey) ? "id." : "";
				String refidStirng1 = (refField.isKey() && isrefMkey) ? "id_" : "";

				code.append("$('#grid_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"').val($(this).parent().parent().children(\"td\")" +
						".eq($('#grid_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"_index').val()).text());\r\n");
			}
			
		}
		
		
		return code.toString();
	}
	
	@FillItem(itemName = "<##FILL_ID2##>")
	public String fillItemTest7(TableInfo tableInfo)
	{
		StringBuffer code= new StringBuffer();
		boolean isMkey=tableInfo.getKeyFields().size()>1;
		
		String idStr=isMkey?"objId.":"";
		for(Map.Entry<String, Field> entry: tableInfo.getAllFields().entrySet())
		{
			Field field=entry.getValue();
			
			String idStirng1=(field.isKey()&&isMkey)?"id_":"";
			String idStirng2=(field.isKey()&&isMkey)?"id.":"";
			
			TypeReference ref = DBToolEngine.getTypeReference_REFOnly(field);

			if (ref == null) {
				code.append("$('#"+tableInfo.getTableId().toLowerCase()+"_"+idStirng1+field.getFieldName()+"').attr('value','');\r\n");
				code.append("$('#"+tableInfo.getTableId().toLowerCase()+"_"+idStirng1+field.getFieldName()+"').attr('value',event.originalEvent.data.obj."+idStirng2+field.getFieldName()+");\r\n");
			} else {
				TableInfo refTable = DBToolEngine.findTable(ref
						.getReferenceId());
				Field refField = refTable.findField(ref.getReferenceField());

				boolean isrefMkey = refTable.getKeyFields().size() > 1;
				String refidStirng2 = (refField.isKey() && isrefMkey) ? "id." : "";
				String refidStirng1 = (refField.isKey() && isrefMkey) ? "id_" : "";

				code.append("$('#"+tableInfo.getTableId().toLowerCase()+"_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"').attr('value','');\r\n");
				code.append("if(event.originalEvent.data.obj."+idStirng2+field.getFieldName()+" !=null){\r\n");
				code.append("	$('#"+tableInfo.getTableId().toLowerCase()+"_"+idStirng1+field.getFieldName()+"_"+refidStirng1+refField.getFieldName()+"').attr('value'," +
						"event.originalEvent.data.obj."+idStirng2+field.getFieldName()+"."+refidStirng2+refField.getFieldName()+");\r\n");
				code.append("}");
			}
			
			
			 
		}
		
		
		return code.toString();
	}
	
	@Override
	public String getFileName(TableInfo tableInfo) {
		return tableInfo.getTableId().toLowerCase()+".jsp";
	}

	@Override
	public String getFilePath(TableInfo tableInfo) {
		return GlobalInfo.PRJ_NAME+"/WebRoot/WEB-INF/content/master";
	}
}
